<?php
session_start();
$char = range('A', 'Z');
$char1 = range('a', 'z');
$num = range(0, 9);
$res = array_merge($char, $num, $char1);
$string = "";
$len = count($res);
for ($i = 0; $i < 4; $i++) {
    $index = rand(0, $len - 1);
    $string = $string . $res[$index];
}
$_SESSION["string"] = $string;

$w = 100;
$h = 25;
header("content-type:image/png");
$img1 = imagecreatetruecolor($w, $h);
$white = imagecolorallocate($img1, 255, 255, 255);
$black = imagecolorallocate($img1, 0, 0, 0);
imagefill($img1, 0, 0, $white);
for ($i = 1; $i <= 20; $i++) {
    imagesetpixel($img1, rand(0, $w - 1), rand(0, $h - 1), $black);
}
for ($i = 0; $i < 2; $i++) {
    imageline($img1, rand(0, $w - 1), rand(0, $h - 1), rand(0, $w - 1), rand(0, $h - 1), $black);
}
$fontfile = "../assets/fonts/times.ttf";
for ($i = 0; $i < 4; $i++) {
    $x = $w / 4 * $i + 8;
    $y = rand(16, 19);
    $color = imagecolorallocate($img1, rand(0, 150), rand(0, 150), rand(0, 150));
    imagettftext($img1, 14, rand(-45, 45), $x, $y, $color, $fontfile, $string[$i]);
}
imagepng($img1);
imagedestroy($img1);

//echo $string;
?>